﻿using System;
using System.Threading.Tasks;

using Hang.Blog.Data;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

using Volo.Abp.DependencyInjection;

namespace Hang.Blog.EntityFrameworkCore {
    public class EntityFrameworkCoreBlogDbSchemaMigrator
        : IBlogDbSchemaMigrator, ITransientDependency {
        private readonly IServiceProvider _serviceProvider;

        public EntityFrameworkCoreBlogDbSchemaMigrator(
            IServiceProvider serviceProvider) {
            _serviceProvider = serviceProvider;
        }

        public async Task MigrateAsync() {
            /* We intentionally resolving the BlogMigrationsDbContext
             * from IServiceProvider (instead of directly injecting it)
             * to properly get the connection string of the current tenant in the
             * current scope.
             */

            await _serviceProvider
                .GetRequiredService<BlogMigrationsDbContext>()
                .Database
                .MigrateAsync();
        }
    }
}